import System from "../System";
import { Rect } from "../utils/Rect";

/** 在摄像机的一定区域进行渲染的辅助优化类。
 * 在非常多资源存在于一个场景的时候需要进行的优化操作
 *
 *  */
export class FocusRender {
    resources: Laya.Sprite[] = [];
    private Looper() {
        this.render(System.Camera.target, this.resources);
    }
    /** 追踪 Camera 进行渲染 */
    trackingToCamera(Resources: Laya.Sprite[]) {
        this.resources = Resources;
        Laya.timer.frameLoop(this.renderFrame, this, this.Looper);
    }
    stopTracking() {
        Laya.timer.clear(this, this.Looper);
    }
    renderFrame = 10;
    renderScale = 1.5;
    /** 进行一次渲染 */
    render(
        focusingCenter: { x: number; y: number },
        handlingResource: Laya.Sprite[]
    ) {
        const { x, y } = focusingCenter;
        const w = (Laya.stage.width / 2) * this.renderScale;
        const h = (Laya.stage.height / 2) * this.renderScale;
        const rect = new Rect(x - w, x + w, y - h, y + h);
        handlingResource.forEach((i) => {
            if (rect.contain(i)) {
                i.set_visible(true);
            } else {
                i.set_visible(false);
            }
        });
    }
}
